package cn.didi.feidi.androidorder.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.didi.feidi.androidorder.Constants;
import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.activity.personal.PersonFindBackPayPasswordGuideActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonJudgeActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonJudgeWatchActivity;
import cn.didi.feidi.androidorder.activity.personal.PersonWalletPayPasswordFirstSetActivity;
import cn.didi.feidi.androidorder.api.Api;
import cn.didi.feidi.androidorder.api.ApiResponse;
import cn.didi.feidi.androidorder.api.ErrorEvent;
import cn.didi.feidi.androidorder.assembly.CommomDialog;
import cn.didi.feidi.androidorder.assembly.ConfirmDepositInfoFragmentDialog;
import cn.didi.feidi.androidorder.assembly.DeleteGoodsFragmentDialog;
import cn.didi.feidi.androidorder.assembly.DeliverInfoFragmentDialog;
import cn.didi.feidi.androidorder.assembly.FeiDiInputPasswordFragmentDialog;
import cn.didi.feidi.androidorder.assembly.FeiDiLoadingFragmentDialog;
import cn.didi.feidi.androidorder.assembly.SelectPayWayFragmentDialog;
import cn.didi.feidi.androidorder.core.ActionCallbackListener;
import cn.didi.feidi.androidorder.core.AppActionRequest;
import cn.didi.feidi.androidorder.databinding.FragmentOrderDetailBinding;
import cn.didi.feidi.androidorder.manager.UserManager;
import cn.didi.feidi.androidorder.model.AuthResult;
import cn.didi.feidi.androidorder.model.OrderUsFi;
import cn.didi.feidi.androidorder.model.OrderUser;
import cn.didi.feidi.androidorder.model.PayResult;
import cn.didi.feidi.androidorder.util.AppUtil;
import cn.didi.feidi.androidorder.util.MD5Util;

/**
 * Created by jinxiong on 2017/9/6.
 */

public class OrderDetailFragment extends FeidiBaseFragment {

    private static final String TAG = "OrderDetailFragment";
    private static final int SDK_PAY_FLAG = 1;
    private static final int SDK_AUTH_FLAG = 2;
    private final static int COMMENT_REQUEST_CODE = 2;

    OrderUser orderUser;
    FragmentOrderDetailBinding binding;


    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SDK_PAY_FLAG: {
                    @SuppressWarnings("unchecked")
                    PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                    /**
                     对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                     */
                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    Log.d(TAG, resultInfo);
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                        makeToast("支付成功");
//                        getDataFromNet(true, false, true);

                    } else {
                        // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                        makeToast("支付失败");
                    }
                    callbackRefresh(true);
                    //刷新页面
//                    getDataFromNet(true, false, true);
                    break;
                }
                case SDK_AUTH_FLAG: {
                    @SuppressWarnings("unchecked")
                    AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
                    String resultStatus = authResult.getResultStatus();

                    // 判断resultStatus 为“9000”且result_code
                    // 为“200”则代表授权成功，具体状态码代表含义可参考授权接口文档
                    if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")) {
                        // 获取alipay_open_id，调支付时作为参数extern_token 的value
                        // 传入，则支付账户为该授权账户
                        makeToast("授权成功");
                    } else {
                        // 其他状态值则为授权失败
                        makeToast("授权失败");

                    }
                    break;
                }
                default:
                    break;
            }
        }

        ;
    };

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */


    public static OrderDetailFragment newInstance(Bundle bundle) {
        OrderDetailFragment fragment = new OrderDetailFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        orderUser = (OrderUser) getActivity().getIntent().getSerializableExtra("orderUser");
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_order_detail, container, false);
        binding.setFragment(this);
        binding.setOrderUser(orderUser);

        return binding.getRoot();
    }

    @Override
    protected void initViews(View rootView) {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void addListener() {
        binding.fragmentOrderFirstTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                firstButtonClickCallback();
            }
        });

        binding.fragmentOrderSecondTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                secondButtonClickCallback();
            }
        });
    }

    public void firstButtonClickCallback() {

        if (isMultiplicationClick()) {
            return;
        }

        switch (orderUser.getStatusUser()) {

            case 0: {
                //待付款的取消订单
                cancelOrder();
                break;
            }

            case 1: {
                cancelOrder();
                break;
            }

            case 2: {
                //待收货的  点击事件配送信息
                lookDeliverInfo();
                break;
            }
            case 5: {
                deleteOrder();
                break;
            }


        }


    }

    public void secondButtonClickCallback() {


        if (isMultiplicationClick()) {
            return;
        }

        switch (orderUser.getStatusUser()) {

            case 0: {
                //待付款的 付款按钮点击事件
                payMoney();
                break;
            }
//            case 1: {
//                //待接收的 取消订单
////                cancelOrder();
//                break;
//            }
            case 2: {

                if (orderUser.isNeedExtractDeposit()) {
                    commitDeposit();
                    //待收货，补交押金
                } else {
//                    Log.d(TAG, "secondButtonClickCallback: " + orderUser.isNeedExtractDeposit());
                    //待收货，确认收货
                    confirmOrder();
                }

                break;
            }

            case 4: {

                if (orderUser.getCommentUserId() == null || orderUser.getCommentUserId().length() == 0) {
                    // 售后   发起评论
                    Intent intent = new Intent(getActivity(), PersonJudgeActivity.class);
                    intent.putExtra("orderUser", orderUser);
                    startActivityForResult(intent, COMMENT_REQUEST_CODE);
                } else {
                    //售后   查看评论
                    Intent intent = new Intent(getActivity(), PersonJudgeWatchActivity.class);
                    intent.putExtra("orderUser", orderUser);
                    startActivityForResult(intent, COMMENT_REQUEST_CODE);

                }

                break;
            }
//            case 5: {
//                //售后   删除 已经取消的订单
////                deleteOrder();
//                break;
//
//            }

        }
    }

    /**
     * 取消订单
     *
     * @param
     */
    //ok
    private void cancelOrder() {

        if (!UserManager.isBingAccount()) {
            return;
        }

        final DeleteGoodsFragmentDialog dialog = new DeleteGoodsFragmentDialog();

        dialog.setLeftMessage("狠心取消");
        dialog.setTitle("取消订单");
        dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Map<String, String> parameters = new HashMap<>();
                parameters.put("userId", UserManager.getUserId());
                parameters.put("token", UserManager.getToken());
                parameters.put("orderUserId", orderUser.getOrderUserId());

                executeRequest(new AppActionRequest<>(Api.CANCEL_ORDER, parameters,
                        new TypeToken<ApiResponse<OrderUser>>() {
                        }.getType(),
                        new ActionCallbackListener<OrderUser>() {
                            @Override
                            public void onSuccess(String message, OrderUser order) {
                                Toast.makeText(mActivity, "取消成功", Toast.LENGTH_SHORT).show();
                                orderUser = order;
                                binding.setOrderUser(order);
                                callbackRefresh(false);

                            }

                            @Override
                            public void onFailure(int errorEvent, String message) {
//                                Log.d(TAG, "onFailure: " + message);
                            }
                        }));
                dialog.dismiss();

            }
        });

        dialog.show(getChildFragmentManager(), "cancelOrder");
    }

    /***
     * 查看 配送信息
     */
    //ok
    private void lookDeliverInfo() {

        DeliverInfoFragmentDialog dialog = new DeliverInfoFragmentDialog();
        Bundle bundle = new Bundle();
        bundle.putSerializable("orderUser", orderUser);
        dialog.setArguments(bundle);

        dialog.show(getChildFragmentManager(), "lookDeliverInfo");

    }

    /**
     * 支付订单
     */
    private void payMoney() {
        final SelectPayWayFragmentDialog selectPayWayFragmentDialog = new SelectPayWayFragmentDialog();
        selectPayWayFragmentDialog.setAliPayListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMultiplicationClick()) {
                    return;
                }
                selectPayWayFragmentDialog.dismiss();

                payMoneyByAli();
            }
        }).setWalletListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isMultiplicationClick()) {
                    return;
                }
                selectPayWayFragmentDialog.dismiss();
                payMoneyByWallet();
            }
        });

        Bundle bundle = new Bundle();
        bundle.putDouble("amount", orderUser.getAmount());
        selectPayWayFragmentDialog.setArguments(bundle);
        selectPayWayFragmentDialog.show(getChildFragmentManager(), "selectPayWay");

    }

    /***
     * 补交押金
     * @param
     */
    private void commitDeposit() {


        final FeiDiLoadingFragmentDialog diLoadingFragmentDialog = new FeiDiLoadingFragmentDialog();
        diLoadingFragmentDialog.show(getChildFragmentManager(), "loading");

        HashMap<String, String> parameters = new HashMap<>();

        parameters.put("userId", UserManager.getUserId());
        parameters.put("token", UserManager.getToken());
        parameters.put("orderUserId", orderUser.getOrderUserId());

        executeRequest(new AppActionRequest<>(Api.GET_ORDERUSER_FINISH, parameters,
                new TypeToken<ApiResponse<OrderUsFi>>() {
                }.getType(), new ActionCallbackListener<OrderUsFi>() {
            @Override
            public void onSuccess(String message, final OrderUsFi data) {

                diLoadingFragmentDialog.dismiss();
                ConfirmDepositInfoFragmentDialog dialog = new ConfirmDepositInfoFragmentDialog();
                dialog.setOrderUsFi(data);
                dialog.setPayListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        payDeposit(data);
                    }
                });

                dialog.show(getChildFragmentManager(), "commitDeposit");
            }

            @Override
            public void onFailure(int errorEvent, String message) {
                if (errorEvent == ErrorEvent.PARAM_ACCESS_ERROR_NET) {
                    Toast.makeText(mActivity, "检查网络", Toast.LENGTH_SHORT).show();
                }
                diLoadingFragmentDialog.dismiss();
            }
        }));


    }

    /**
     * 补交押金 ,需要请求另外的数据，拿到补交的押金数和 缺德空桶数
     *
     * @param
     */
    private void payDeposit(final OrderUsFi data) {

        final SelectPayWayFragmentDialog selectPayWayFragmentDialog = new SelectPayWayFragmentDialog();
        selectPayWayFragmentDialog.setAliPayListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMultiplicationClick()) {
                    return;
                }
                selectPayWayFragmentDialog.dismiss();
                ((DialogFragment) getChildFragmentManager().findFragmentByTag("commitDeposit")).dismiss();

                //  2017/8/30 支付宝支付 ---jinXiong
                payDepositByAli(data);
            }
        }).setWalletListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (isMultiplicationClick()) {
                    return;
                }
                selectPayWayFragmentDialog.dismiss();
                ((DialogFragment) getChildFragmentManager().findFragmentByTag("commitDeposit")).dismiss();

                //  2017/8/30 钱包支付 ---jinXiong
//                Toast.makeText(mActivity, "钱包支付", Toast.LENGTH_SHORT).show();
                payDepositByWallet(data);
            }
        }).setCloseListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((DialogFragment) getChildFragmentManager().findFragmentByTag("commitDeposit")).
                        getDialog().show();
                selectPayWayFragmentDialog.dismiss();
            }
        });


        Bundle bundle = new Bundle();

        //这个不对
        bundle.putDouble("amount", data.getUserDeposit());
        selectPayWayFragmentDialog.setArguments(bundle);

        ((DialogFragment) getChildFragmentManager().findFragmentByTag("commitDeposit")).getDialog().hide();
        selectPayWayFragmentDialog.show(getChildFragmentManager(), "selectPayWay");
    }

    /**
     * 支付订单 支付宝支付
     *
     * @param
     */
    //ok
    private void payMoneyByAli() {
        Map<String, String> params = new HashMap<>();

        params.put("userId", UserManager.getUserId());
        params.put("token", UserManager.getToken());
        params.put("orderUserId", orderUser.getOrderUserId());
        params.put("amount", String.valueOf(orderUser.getAmount()));

        executeRequest(new AppActionRequest<String>(Api.GET_ALIPAY_SIGNED_ORDER_USER, params,
                new TypeToken<ApiResponse<String>>() {
                }.getType(), new ActionCallbackListener<String>() {
            @Override
            public void onSuccess(String message, final String data) {
                if (data != null) {
                    Runnable payRunnable = new Runnable() {

                        @Override
                        public void run() {
                            PayTask alipay = new PayTask(mActivity);
                            Map<String, String> result = alipay.payV2(data, true);
                            Log.i("jinxiong", result.toString());

                            Message msg = Message.obtain();
                            msg.what = SDK_PAY_FLAG;
                            msg.obj = result;
                            mHandler.sendMessage(msg);
                        }
                    };
                    // 必须异步调用
                    Thread payThread = new Thread(payRunnable);
                    payThread.start();
                } else {
                    makeToast("success" + message);
                    Log.d(TAG, message);
                }
            }

            @Override
            public void onFailure(int errorEvent, String message) {
                Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);

                switch (errorEvent) {
                    default:
                        makeToast("未知错误");
                        break;
                }
            }
        }));
    }

    /**
     * 支付订单 钱包支付
     *
     * @param
     */
    //ok
    private void payMoneyByWallet() {
        if (!UserManager.isSetPayPassword()) {//未设置支付密码
            new CommomDialog(getContext(), R.style.dialog, "您尚未设置支付密码", new CommomDialog.OnCloseListener() {
                @Override
                public void onClick(Dialog dialog, boolean confirm) {
                    if (confirm) {
                        makeToast("现在设置");
                        Intent intent = new Intent(getContext(), PersonWalletPayPasswordFirstSetActivity.class);
                        getContext().startActivity(intent);
                        dialog.dismiss();
                    }
                }
            }, true, true).setPositiveButton("现在设置").show();
        } else {
            final FeiDiInputPasswordFragmentDialog inputPasswordFragmentDialog = new FeiDiInputPasswordFragmentDialog();

            inputPasswordFragmentDialog.setCallback(new FeiDiInputPasswordFragmentDialog.Callback() {
                @Override
                public void inputCallback(String inputString) {
                    //输入完 6 位密码后自动回调 发起支付
                    Map<String, String> params = new HashMap<>();

                    params.put("userId", UserManager.getUserId());
                    params.put("token", UserManager.getToken());
                    params.put("orderUserId", orderUser.getOrderUserId());
                    params.put("payPwd", MD5Util.GetMD5Code32Bit(inputString));

                    executeRequest(new AppActionRequest<List<OrderUser>>(Api.FEIDI_PAY_PAY_ORDER_USER, params,
                            new TypeToken<ApiResponse<List<OrderUser>>>() {
                            }.getType(), new ActionCallbackListener<List<OrderUser>>() {
                        @Override
                        public void onSuccess(String message, final List<OrderUser> data) {
                            makeToast("支付成功");
                            orderUser = data.get(0);
                            binding.setOrderUser(data.get(0));
                            callbackRefresh(false);
                            inputPasswordFragmentDialog.dismiss();

//                            getDataFromNet(true, false, true);
                        }

                        @Override
                        public void onFailure(int errorEvent, String message) {
                            Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
                            switch (errorEvent) {
                                case ErrorEvent.WRONG_PAYPWD:
//                                  支付密码错误
                                    makeToast(message);
                                    inputPasswordFragmentDialog.clearInput();
                                    break;
                                case ErrorEvent.UNSET_PAYPWD:
                                    makeToast("支付密码未设置");
                                    inputPasswordFragmentDialog.dismiss();
                                    break;
                                case ErrorEvent.FEIDI_WALLET_PAY_PASSWORD_BEYOND_FIVE_TIME_IN_ONE_HOUR:
                                    new CommomDialog(getContext(), R.style.dialog, "密码错误次数已达上限\n请一小时后重试", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            if (confirm){
                                                dialog.dismiss();
                                                inputPasswordFragmentDialog.dismiss();
                                            }
                                        }
                                    },true,true).setPositiveButton("好的").show();
                                    break;
                                default:
                                    makeToast("未知错误");
                                    inputPasswordFragmentDialog.dismiss();
                                    break;
                            }
                        }
                    }));

                }
            }).setForgetPassworListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //忘记密码
                    Intent intent = new Intent(getContext(), PersonFindBackPayPasswordGuideActivity.class);
                    startActivity(intent);
                }
            });

            inputPasswordFragmentDialog.show(getChildFragmentManager(), "payMoneyByWallet");
        }

    }

    /**
     * 删除已经完成/取消的订单 ，订单的 状态为 4 ， 5
     *
     * @param
     */
    //ok
    private void deleteOrder() {
        if (!UserManager.isBingAccount()) {
            return;
        }

        final DeleteGoodsFragmentDialog dialog = new DeleteGoodsFragmentDialog();

        dialog.setLeftMessage("删除订单");
        dialog.setTitle("删除后不可再找回");
        dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Map<String, String> parameters = new HashMap<>();
                parameters.put("userId", UserManager.getUserId());
                parameters.put("token", UserManager.getToken());
                parameters.put("orderUserId", orderUser.getOrderUserId());

                executeRequest(new AppActionRequest<>(Api.DELETE_CANCEL_OR_COMPLETE_ORDER, parameters,
                        new TypeToken<ApiResponse<OrderUser>>() {
                        }.getType(),
                        new ActionCallbackListener<OrderUser>() {
                            @Override
                            public void onSuccess(String message, OrderUser order) {
                                Toast.makeText(mActivity, "删除成功", Toast.LENGTH_SHORT).show();
                                orderUser = order;
                                callbackRefresh(true);
//                                getDataFromNet(true, false, true);
//                                adapter.removeOrderUsers(orderUser)
                            }

                            @Override
                            public void onFailure(int errorEvent, String message) {
                                Log.d(TAG, "onFailure: " + message);
                            }
                        }));
                dialog.dismiss();

            }
        });

        dialog.show(getChildFragmentManager(), "deleteOrder");


    }

    /**
     * 确认收货
     *
     * @param
     */
    //ok
    private void confirmOrder() {


        Log.d(TAG, "confirmOrder:UserManager   " + !UserManager.isBingAccount());

        if (!UserManager.isBingAccount()) {
            return;
        }


        Log.d(TAG, "confirmOrder: " + orderUser.isNeedExtractDeposit());

        final DeleteGoodsFragmentDialog dialog = new DeleteGoodsFragmentDialog();

        dialog.setLeftMessage("确认送达");
        dialog.setTitle("是否确认送达");
        dialog.setRightMessage("取消");
        dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                Map<String, String> parameters = new HashMap<>();
                parameters.put("userId", UserManager.getUserId());
                parameters.put("token", UserManager.getToken());
                parameters.put("orderUserId", orderUser.getOrderUserId());

                executeRequest(new AppActionRequest<>(Api.CONFIRM_ORDER, parameters,
                        new TypeToken<ApiResponse<OrderUser>>() {
                        }.getType(),
                        new ActionCallbackListener<OrderUser>() {
                            @Override
                            public void onSuccess(String message, OrderUser orderUser) {
//                                Toast.makeText(mActivity, "删除成功", Toast.LENGTH_SHORT).show();
//                                getDataFromNet(true, false, true);
                                OrderDetailFragment.this.orderUser = orderUser;
                                binding.setOrderUser(orderUser);
                                callbackRefresh(false);

                            }

                            @Override
                            public void onFailure(int errorEvent, String message) {
                                Log.d(TAG, "onFailure: " + message);
                                switch (errorEvent){
                                    case ErrorEvent.ORDERUSER_HAVE_NOT_BEEN_CONFIRM_BY_WATER_DELIVER:
                                        makeToast("等待送水工确认");
                                        break;
                                    case ErrorEvent.ORDERUSER_NEED_DEPOSIT:
                                        makeToast("需要补交押金");
                                        commitDeposit();
                                }
                            }
                        }));
                dialog.dismiss();

            }
        });

        dialog.show(getChildFragmentManager(), "confirmOrder");
    }

    /**
     * 押金支付 支付宝
     */
    //ok
    private void payDepositByAli(OrderUsFi orderUsFi) {
        Map<String, String> params = new HashMap<>();

        params.put("userId", UserManager.getUserId());
        params.put("token", UserManager.getToken());
        params.put("orderUserId", orderUsFi.getOrderUserId());
        params.put("amount", String.valueOf(orderUsFi.getUserDeposit()));

        executeRequest(new AppActionRequest<String>(Api.GET_ALIPAY_SIGNED_DEPOSIT, params,
                new TypeToken<ApiResponse<String>>() {
                }.getType(), new ActionCallbackListener<String>() {
            @Override
            public void onSuccess(String message, final String data) {
                if (data != null) {
                    Runnable payRunnable = new Runnable() {

                        @Override
                        public void run() {
                            PayTask alipay = new PayTask(mActivity);
                            Map<String, String> result = alipay.payV2(data, true);
                            Log.i("jinxiong", result.toString());

                            Message msg = Message.obtain();
                            msg.what = SDK_PAY_FLAG;
                            msg.obj = result;
                            mHandler.sendMessage(msg);
                        }
                    };
                    // 必须异步调用
                    Thread payThread = new Thread(payRunnable);
                    payThread.start();
                } else {
                    makeToast("success" + message);
                    Log.d(TAG, message);
                }
            }

            @Override
            public void onFailure(int errorEvent, String message) {
                Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
                switch (errorEvent) {
                    default:
                        makeToast("支付失败");
                        break;
                }
            }
        }));
    }

    /**
     * 押金支付 钱包
     *
     * @param orderUsFi
     */
    //ok
    private void payDepositByWallet(final OrderUsFi orderUsFi) {
        //检测是否设定支付密码
        if (!UserManager.isSetPayPassword()) {//未设置支付密码
            new CommomDialog(getContext(), R.style.dialog, "您尚未设置支付密码", new CommomDialog.OnCloseListener() {
                @Override
                public void onClick(Dialog dialog, boolean confirm) {
                    if (confirm) {
                        makeToast("现在设置");
                        Intent intent = new Intent(getContext(), PersonWalletPayPasswordFirstSetActivity.class);
                        getContext().startActivity(intent);
                        dialog.dismiss();
                    }
                }
            }, true, true).setPositiveButton("现在设置").show();
        } else {
            final FeiDiInputPasswordFragmentDialog inputPasswordFragmentDialog = new FeiDiInputPasswordFragmentDialog();

            inputPasswordFragmentDialog.setCallback(new FeiDiInputPasswordFragmentDialog.Callback() {
                @Override
                public void inputCallback(String inputString) {
                    //输入完 6 位密码后自动回调 发起支付
                    Map<String, String> params = new HashMap<>();

                    params.put("userId", UserManager.getUserId());
                    params.put("token", UserManager.getToken());
                    params.put("orderUserId", orderUsFi.getOrderUserId());
                    params.put("payPwd", MD5Util.GetMD5Code32Bit(inputString));

                    executeRequest(new AppActionRequest<String>(Api.FEIDI_PAY_PAY_EXTRA_DEPOSIT, params,
                            new TypeToken<ApiResponse<String>>() {
                            }.getType(), new ActionCallbackListener<String>() {
                        @Override
                        public void onSuccess(String message, final String data) {
                            makeToast("支付成功");
                            callbackRefresh(true);
                            inputPasswordFragmentDialog.dismiss();
//                            getDataFromNet(true, false, true);
                        }

                        @Override
                        public void onFailure(int errorEvent, String message) {
                            Log.d(TAG, "errorEvent:" + errorEvent + "message=" + message);
                            switch (errorEvent) {
                                case ErrorEvent.WRONG_PAYPWD:
//                                  支付密码错误
                                    makeToast(message);
                                    inputPasswordFragmentDialog.clearInput();
                                    break;
                                case ErrorEvent.UNSET_PAYPWD:
                                    makeToast("支付密码未设置");
                                    inputPasswordFragmentDialog.dismiss();
                                    break;
                                case ErrorEvent.ORDERUSER_DONT_NEED_DEPOSIT:
                                    makeToast("已补交押金完成");
                                    inputPasswordFragmentDialog.dismiss();
                                    break;
                                case ErrorEvent.FEIDI_WALLET_PAY_PASSWORD_BEYOND_FIVE_TIME_IN_ONE_HOUR:
                                    new CommomDialog(getContext(), R.style.dialog, "密码错误次数已达上限\n请一小时后重试", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            if (confirm){
                                                dialog.dismiss();
                                                inputPasswordFragmentDialog.dismiss();
                                            }
                                        }
                                    },true,true).setPositiveButton("好的").show();
                                    break;
                                default:
                                    makeToast("未知错误");
                                    inputPasswordFragmentDialog.dismiss();
                                    break;
                            }
                        }
                    }));
                }
            }).setForgetPassworListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //忘记密码
                    Intent intent = new Intent(getContext(), PersonFindBackPayPasswordGuideActivity.class);
                    startActivity(intent);
                }
            });

            inputPasswordFragmentDialog.show(getChildFragmentManager(), "payDepositByWallet");
        }

    }

    private void callbackRefresh(boolean finish) {

        Activity activity = getActivity();
        Intent intent = new Intent();
        intent.putExtra("refresh", true);
        activity.setResult(2, intent);

        if (true) {
            activity.finish();
        } else {
            binding.setFragment(this);
        }
    }

    /*****************************************************/
    /****
     *
     *  public static final int STATUS_USER_PAYING = 0;//待支付
     *  public static final int STATUS_USER_RECEIVING = 1;//待接单
     *  public static final int STATUS_USER_TAKING = 2;//待收货
     *  public static final int STATUS_USER_FINISHED = 4;//已完成
     *  public static final int STATUS_USER_CANCELED = 5;//已取消
     *
     */
    public String getOrderStatus() {


        switch (orderUser.getStatusUser()) {

            case 0: {

                return "等待买家付款";
            }
            case 1: {
                return "等待接单";
            }


            case 2: {
                return "等待买家收货";
            }

            case 4: {
                return "订单已完成";
            }
            case 5: {
                return "订单已关闭";
            }

        }
        return "";

    }

    public String getTimeString() {

        String startTime = AppUtil.dateTransferString(orderUser.getStartTime(), Constants.START_TIME_FORMAT);
        String endTime = AppUtil.dateTransferString(orderUser.getEndTime(), Constants.START_TIME_FORMAT);
        return startTime + "-" + endTime;
    }

    public String getGoodsPrice() {

        List<OrderUser.OrderWaBuOrUsListBean> bean = orderUser.getOrderWaBuOrUsList();

        int size = bean.size();

        float price = 0;

        for (int i = 0; i < size; i++) {
            price += bean.get(i).getUserPriceTotal();
        }

        return "¥" + price;


    }

    public String getPayDepositString() {

        return '¥' + String.valueOf(orderUser.getDeposit());
    }

    public String getPayWayString() {
        /**
         * 支付方式pay_way
         */
//        public static final int FEIDI = 0;/*飞滴钱包支付*/
//        public static final int WECHAT = 1; /*微信支付*/
//        public static final int ALIPAY = 2;/*支付宝支付*/

        if (orderUser.getPayWay() == null) {
            return "";
        }

        switch (orderUser.getPayWay()) {

            case "0": {
                return "飞滴钱包";
            }

            case "2": {
                return "支付宝";
            }
        }

        return "";

    }

    /****
     *
     *  public static final int STATUS_USER_PAYING = 0;//待支付
     *  public static final int STATUS_USER_RECEIVING = 1;//待接单
     *  public static final int STATUS_USER_TAKING = 2;//待收货
     *  public static final int STATUS_USER_FINISHED = 4;//已完成
     *  public static final int STATUS_USER_CANCELED = 5;//已取消
     *
     */
    public String getFirstTvString() {

        switch (orderUser.getStatusUser()) {

            case 0: {
                return "取消订单";
            }
            case 1: {
                return "取消订单";
            }

            case 2: {
                return "查看进度";
            }

            case 4: {
                return "投诉";
            }
            case 5: {
                return "删除订单";
            }

        }
        return "";

    }

    /****
     *
     *  public static final int STATUS_USER_PAYING = 0;//待支付
     *  public static final int STATUS_USER_RECEIVING = 1;//待接单
     *  public static final int STATUS_USER_TAKING = 2;//待收货
     *  public static final int STATUS_USER_FINISHED = 4;//已完成
     *  public static final int STATUS_USER_CANCELED = 5;//已取消
     *
     */
    public String getSecondTvString() {
        switch (orderUser.getStatusUser()) {

            case 0: {
                return "付款";
            }
            case 1: {
                break;
            }

            case 2: {

                if (orderUser.isNeedExtractDeposit()) {
                    return "补交押金";
                } else {
                    return "确认收货";
                }
            }

            case 4: {
                if (orderUser.getCommentUserId() != null && orderUser.getCommentUserId().length() > 0) {
                    return "查看评论";
                } else {
                    return "评论";
                }
            }
            case 5: {
                break;
            }

        }
        return "";
    }

    public int getSecondTvVisible() {

        switch (orderUser.getStatusUser()) {


            case 1: {

            }
            case 5: {
                return View.GONE;
            }

        }
        return View.VISIBLE;
    }

}
